const moment = require('moment')
const { VIP_COUNTDOWN_DAYS } = require('../../config/constant')
const dict = require('../../config/dict')

/**
 * 获取应用类型标识
 * @param {Number} appType 0、年会员 1、天会员
 * @returns 
 */
function getAppTypeKey(appType) {
    return appType == 0 ? 'YEAR' : 'DAY'
}

/**
 * 设备格式化(兼容项目代码)
 * @param {String}} device 
 */
function deviceDataFormat(device, appType) {
    try {
        const appTypeKey = getAppTypeKey(appType)

        device.deviceId = device.id;
        device.deviceName = device.name;
        device.deviceCode = appTypeKey + device.mac.replace(/:/g, '').toLocaleUpperCase();

        device.projectId = device.hotelId;
        device.projectName = device.hotelName;
        device.projectCode = appTypeKey + device.hotelId;

        device.companyId = device.coId;
        device.companyName = device.coName
        device.companyCode = appTypeKey + device.coId;

        device.registerTypeDesc = dict.registerTypeValues[device.registerType] || '未知'

        device.createTime = moment(device.createTime).format('YYYY-MM-DD HH:mm:ss')
        device.updateTime = moment(device.updateTime).format('YYYY-MM-DD HH:mm:ss')
        if (device.dateSVIP) {
            device.dateSVIP = moment(device.dateSVIP).format('YYYY-MM-DD HH:mm:ss')
            device.vipEndTime = device.dateSVIP

            // 年会员判断还有多久过期
            if (device.vipType == 0) {
                const days = moment(device.dateSVIP).diff(moment(), 'days')
                if (device.dateSVIP < moment().format('YYYY-MM-DD HH:mm:ss')) device.isExpire = true // 已过期
                else if (days < VIP_COUNTDOWN_DAYS) device.willExpire = true // 即将过期
            }
        }

        if (device.vipType == undefined) device.vipType = 1;

        device.vipTypeDesc = {
            0: '年会员',
            1: '天会员'
        }[device.vipType]
    } catch (e) {
        console.error(e);
    }

    return device;
}
module.exports = {
    getAppTypeKey,
    deviceDataFormat,
}
